package negocios.gerenciadores;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

import negocios.cinemaBeans.*;

public class Bilheteria implements IBilheteria {
	
	public void vendeIngresso(Exibicao exibicao, Sala sala, Ingresso ingresso) {
		if(verificaExibicao(exibicao, sala)){
			exibicao.adicionaIngresso(ingresso);

		}
	}
	
	
	protected boolean verificaExibicao(Exibicao exibicao, Sala sala){
		for (Exibicao buscaExibicao: sala.getExibicao()){
			if(buscaExibicao.equals(exibicao) && 
					sala.getQtdAssentos()>exibicao.qtdIngressos(sala)){
				return true;
			}
		}
		return false;
	}


	public void imprimeIngresso(ArrayList<Ingresso> listaIngresso){
		
			File f = new File("Ingresso.txt");
			
			if (!f.exists()){
				try{
					f.createNewFile();	
				}
				catch(IOException io){
					io.getMessage();
				}
				
			}
			
				try{
					
					FileWriter w = new FileWriter(f);
					PrintWriter p = new PrintWriter(w);
					
					for (Ingresso ingresso : listaIngresso){			
						w.append(ingresso.toString());
						p.println(w.toString());
					}
					w.close();
				}
				catch(IOException io){
					io.getMessage();
				}
				
			}
	


}
